from mysql_learn.orm_sqlalchemy.many_to_many import orm_m2m
from sqlalchemy.orm import sessionmaker

Session_class = sessionmaker(orm_m2m.engine)    #这两条死记住，先创建与数据库的会话session_class这是个类
session = Session_class()   #实例化这个类

#新增数据
# book1 = orm_m2m.Book(name='少年阿斌', pub_date='2021-10-21')
# book2 = orm_m2m.Book(name='少妇白洁', pub_date='2021-10-20')
# book3 = orm_m2m.Book(name='小黄书', pub_date='2021-10-19')
#
# author1 = orm_m2m.Author(name='刘奇')
# author2 = orm_m2m.Author(name='马明伦')
# author3 = orm_m2m.Author(name='强哥')
#
# book1.authors = [author1, author2]
# book2.authors = [author1, author2, author3]
#
# session.add_all([book1, book2, book3, author1, author2, author3])

#查
# author_obj = session.query(orm_m2m.Author).filter(orm_m2m.Author.name=='刘奇').first()
# print(author_obj.books)

book_obj = session.query(orm_m2m.Book).filter(orm_m2m.Book.name=='少年阿斌').first()
print(book_obj.authors)

session.commit()